package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 展示购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //查询
        List<ShoppingCart> list = shoppingCartService.list(wrapper);

        return R.success(list);
    }

    /**
     * 添加到购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加的信息是：{}", shoppingCart);
        //添加用户id信息，指定是哪个用户的购物车
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //判断这个菜品或者套餐是否已存在
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);

        //判断是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            //添加的是菜品
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //SQL:selete * from shopping_cart where user_id = ? and dish_id = ?
        ShoppingCart shoppingOne = shoppingCartService.getOne(wrapper);
        if (shoppingOne != null) {
            //如果存在，将该菜品或套餐数量加一
            Integer number = shoppingOne.getNumber();
            shoppingOne.setNumber(number + 1);
            //SQL:update from shopping_cart set(...) value(...)
            shoppingCartService.updateById(shoppingOne);
        } else {
            //不存在，则创建新的，数量设置为一
            shoppingCart.setNumber(1);
            //SQL:insert  into shopping_cart(...) value(....)
            shoppingCartService.save(shoppingCart);
            shoppingOne = shoppingCart;
        }
        return R.success(shoppingOne);
    }

    /**
     * 从购物车去除（按照数量）
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //菜品id
        Long dishId = shoppingCart.getDishId();
        //套餐id
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //判断是否是菜品，并查询菜品id
        wrapper.eq(dishId != null, ShoppingCart::getDishId, dishId);
        //判断是否是套餐，并查询套餐id
        wrapper.eq(setmealId != null, ShoppingCart::getSetmealId, setmealId);

        //修改购物车中的number数量，减一
        ShoppingCart shoppingOne = shoppingCartService.getOne(wrapper);
        if (shoppingOne == null){
            throw new CustomException("当前数量为0，请刷新");
        }
        Integer number = shoppingOne.getNumber();
        if (number == 1){
            //说明数量只剩一个了，则直接从购物车删除该条数据
            shoppingCartService.remove(wrapper);
            shoppingOne.setNumber(0);
        }else {
            //数量大于1，减一即可
            shoppingOne.setNumber(number - 1);
            shoppingCartService.updateById(shoppingOne);
        }

        return R.success(shoppingOne);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> delete(){
        log.info("清空购物车...");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //清空当前用户的购物车
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //SQL:delete from shopping_cart where user_id = ?
        shoppingCartService.remove(wrapper);
        return R.success("清空成功");
    }
}
